package com.orangelife.mobile.hourlyEmployee.biz;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.hourlyEmployee.activity.PlaceOrderActivity;
import com.orangelife.mobile.hourlyEmployee.adapter.SingleCheckedListAdapter;
import com.orangelife.mobile.util.StringUtil;
import com.orangelife.mobile.util.ToastHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** 钟点工服务时间的业务处理 */
public class ValuePicker extends LinearLayout{
	
	private Context mContext;
	private LayoutInflater mInflater;
	private ListView lvLeft, lvRight;
			
	private String[] date;
	private String[] full;
	private Map<String, String[]> details = new HashMap<String, String[]>();
	
	/** 被选中的item的位置和item的值 */
	private int mPosLeft = -1;	
	private String mCurLeft;
	private int mPosRight = -1;
	private String mCurRight;
	
	List<Map<String, Object>> listInfo = new ArrayList<Map<String, Object>>();
	
	private String minDayTime;
	private String maxDayTime;
	
	private int temp;
	private float minTime;
	private String[] strTime;
	
	/** 选中的时间 */
	public static String time;
	/** 选中的小时数 */
	public static String hourNum;
	/** 选中时间是否已满 */
	public static String isFull;
	/**
	 * Listener to handle the logic when current city card number is selected
	 */
	public OnClickListener mListener;
	
	/**
	 * @param listener the listener to set
	 */
	public void setButtonOnClickListener(OnClickListener listener) {
		this.mListener = listener;
	}

	public ValuePicker(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}
	
	public ValuePicker(Context context) {
		super(context);
		init(context);
	}
	
	private void init(Context context){
		mContext = context;		
	}
	
	public void initialize(){
		initData();		
		initView();
	}
	
	/** 初始化数据 */
	private void initData(){		
		listInfo = PlaceOrderActivity.mapTime;
		date = new String[listInfo.size()];
		full = new String[listInfo.size()];
		/** 左边listview的数据 */
		for(int i = 0; i < listInfo.size(); i++) {
			date[i] = listInfo.get(i).get("timeInfo").toString();
			full[i] = listInfo.get(i).get("isFull").toString();
			minDayTime = listInfo.get(i).get("minDayTime").toString();
			maxDayTime = listInfo.get(i).get("maxDayTime").toString();
			temp = (int) ((Float.parseFloat(maxDayTime) - Float.parseFloat(minDayTime))*2+1);
			minTime = (float) (Float.parseFloat(minDayTime)-0.5);
			strTime = new String[temp];
			/** 右边listview的数据 */
			for(int j = 0; j < temp; j++) {
				minTime += 0.5;
				strTime[j] = minTime+"小时";
			}
			details.put(listInfo.get(i).get("timeInfo").toString(), strTime); 
		}
		
		int len = date.length;
		
		for(int i = 0; i < len; i++){
			
			String summary = date[i];
			
			if(summary.equals(mCurLeft)){
				mPosLeft = i;
				break;
			}		
		}
		
		if(mPosLeft >= 0){
			String summary = date[mPosLeft];
			String[] right = details.get(summary);
			int lenOfRight = right.length;
			
			for(int j = 0; j < lenOfRight; j++){
				
				String detail = right[j];
				
				if(mCurRight != null && detail.equals(mCurRight)){
					mPosRight = j;
					break;
				}
			}
		}
	}
	
	private void initView(){
		mInflater = LayoutInflater.from(mContext);
		View parent = mInflater.inflate(R.layout.custom_twofold_listview, this);
		
		lvLeft = (ListView) parent.findViewById(R.id.lvLeft);
		lvRight = (ListView) parent.findViewById(R.id.lvRight);
		
		final SingleCheckedListAdapter lAdapter = new SingleCheckedListAdapter(mContext, date, full, 0);
		lAdapter.setCheckedPosition(mPosLeft);
		lvLeft.setAdapter(lAdapter);
		
		/** 加载右边listview数据 */
		String[] rights = new String[]{};
		if(mPosLeft >= 0 && mPosRight >= 0){
			rights = details.get(date[mPosLeft]);
		}else if(mPosLeft == -1 && mPosRight == -1) {
			rights = details.get(date[0]);
		}
		
		final SingleCheckedListAdapter rAdapter = new SingleCheckedListAdapter(mContext, rights, full, 1);		
		rAdapter.setCheckedPosition(mPosRight);	
		lvRight.setAdapter(rAdapter);		
		
		/** 左边listview的监听事件 */
		lvLeft.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
				
				mCurRight = null;
				lAdapter.setCheckedPosition(position);
				lAdapter.setCheckedView(view);
				mPosLeft = position;
				rAdapter.setData(details.get(date[mPosLeft]));
				time = listInfo.get(position).get("time").toString();
				isFull = listInfo.get(position).get("isFull").toString();
				hourNum = "";
			}
		});

		/** 右边listview的监听事件 */
		lvRight.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
				if(StringUtil.isContainBlank(time)) {
					ToastHelper.getInstance()._toast("请先选择日期");
				} else {
					rAdapter.setCheckedPosition(position);
					rAdapter.setCheckedView(view);
					mCurRight = details.get(date[mPosLeft])[position];
					hourNum = mCurRight;
				}
			}
		});		
		
	}
	
	public String getLeftVaue(){
		if(mPosLeft >= 0){
			return date[mPosLeft];
		}else{
			return "";
		}
	}
	
	public void setLeftValue(String leftValue){
		mCurLeft = leftValue;
	}
	
	public String getRightValue(){
		return mCurRight;
	}
	
	public void setRightValue(String rightValue){
		mCurRight  = rightValue;
	}

}
